package com.anlogic.sdk.bootimage.internal.ui;

import com.anlogic.sdk.bootimage.internal.ui.common.IUIValidator;
import com.anlogic.sdk.bootimage.internal.ui.composite.PartitionConfigComposite;
import com.anlogic.sdk.bootimage.model.BootGenModel;
import com.anlogic.sdk.bootimage.model.Partition;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PartitionConfigDialog extends TitleAreaDialog {
	protected Partition partition;

	protected BootGenModel model;

	private PartitionConfigComposite partitionComposite;

	private boolean editMode;

	public PartitionConfigDialog(Shell parentShell, Partition partition, BootGenModel model) {
		super(parentShell);
		setShellStyle(getShellStyle() | 0x10);
		this.partition = partition;
		this.model = model;
	}

	public void setEditMode(boolean flag) {
		this.editMode = flag;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Add partition");
	}

	protected Control createDialogArea(Composite parent) {
		if (this.editMode) {
			setTitle("Edit the boot image partition");
			setMessage("Edit the boot image partition");
			getShell().setText("Edit Parition");
		} else {
			setTitle("Add new boot image partition");
			setMessage("Add new boot image partition");
		}
		this.partitionComposite = createMainComposite(parent);
		GridDataFactory.fillDefaults().grab(true, true).applyTo((Control) this.partitionComposite);
		this.partitionComposite.refreshUI();
		this.partitionComposite.addListeners();
		this.partitionComposite.setUiValidator(new IUIValidator() {
			public void validate() {
				PartitionConfigDialog.this.validateModel();
			}
		});
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				PartitionConfigDialog.this.validateModel();
			}
		});
		return (Control) this.partitionComposite;
	}

	protected PartitionConfigComposite createMainComposite(Composite parent) {
		PartitionConfigComposite partitionconfigComposite = new PartitionConfigComposite(parent, this.model,
				this.partition);
		return partitionconfigComposite;
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, 0, IDialogConstants.OK_LABEL, true);
		createButton(parent, 1, IDialogConstants.CANCEL_LABEL, false);
	}

	protected Control createButtonBar(Composite parent) {
		Control c = super.createButtonBar(parent);
		setOkButtonEnabled(this.editMode);
		return c;
	}

	protected void setOkButtonEnabled(boolean en) {
		Button okButton = getButton(0);
		if (okButton != null)
			okButton.setEnabled(en);
	}

	protected Point getInitialSize() {
		double multiplier = 0.6D;
		Rectangle bounds = Display.getDefault().getBounds();
		int hHint = (int) (bounds.width * multiplier * 0.7D);
		int vHint = (int) (bounds.height * multiplier);
		return new Point(hHint, vHint);
	}

	protected void okPressed() {
		if (this.partitionComposite != null)
			this.partitionComposite.okPressed();
		super.okPressed();
	}

	protected void validateModel() {
		setErrorMessage(null);
		setOkButtonEnabled(false);
		String filePath = this.partition.getFilePath();
		checkFileEmpty(filePath, "Partition file");
		if (getErrorMessage() != null)
			return;
		checkFileExists(filePath, "Partition file");
		if (getErrorMessage() != null)
			return;
		long offSet = checkForValidNumber(this.partition.getOffSet());
		long alignment = checkForValidNumber(this.partition.getAlignment());
		long load = checkForValidNumber(this.partition.getLoad());
		long exec = checkForValidNumber(this.partition.getExec());
		if (offSet == -1L)
			setErrorMessage("Invalid offset value");
		if (alignment == -1L)
			setErrorMessage("Invalid alignment value");
		if (exec == -1L)
			setErrorMessage("Invalid exec value");
		if (load == -1L)
			setErrorMessage("Invalid load value");
		if (getErrorMessage() == null)
			setOkButtonEnabled(true);
	}

	private long checkForValidNumber(String str) {
		if (str == null || str.isEmpty())
			return 0L;
		try {
			if (str.startsWith("0x"))
				return Long.parseLong(str.substring(2), 16);
			return Long.parseLong(str);
		} catch (Exception exception) {
			return -1L;
		}
	}

	protected void checkFileEmpty(String file_path, String name) {
		if (file_path.isEmpty())
			setErrorMessage(String.valueOf(name) + " path is empty");
	}

	protected void checkFileExists(String file_path, String name) {
		IPath iPath;
		if (file_path.isEmpty())
			return;
		Path path = new Path(file_path);
		if (!path.isAbsolute()) {
			iPath = getRelativePath().append((IPath) path);
		} else {
			iPath = (IPath) path;
		}
		if (!iPath.toFile().exists()) {
			setErrorMessage(String.valueOf(name) + " does not exists");
			return;
		}
		if (!iPath.toFile().isFile()) {
			setErrorMessage(String.valueOf(name) + " path is a directory");
			return;
		}
	}

	private IPath getRelativePath() {
		return (new Path(this.model.getBifFile())).removeLastSegments(1);
	}
}
